<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-input placeholder="请输入内容"
                    class="input-with-select"
                    clearable
                    v-model="queryInfo.query"
                    @clear="getUserList()">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getUserList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">+新增</el-button>
        </el-col>
      </el-row>

      <el-table :data="userlist"
                border
                style="width: 100%">

        <el-table-column fixed
                         type="index"
                         label="#">
        </el-table-column>
        <el-table-column prop="username"
                         label="姓名"
                         width="120">
        </el-table-column>
        <el-table-column prop="email"
                         label="E-mail"
                         width="180">
        </el-table-column>
        <el-table-column prop="mobile"
                         label="电话"
                         width="120">
        </el-table-column>
        <el-table-column prop="role_name"
                         label="权限组"
                         width="120">
        </el-table-column>
        <el-table-column label="状态"
                         width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state"
                       @change="userStateChanged(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time"
                         label="注册日期"
                         width="150">
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="180">
          <template slot-scope="scope">
            <el-button type="primary"
                       @click="userEdit(scope.row.id)"
                       icon="el-icon-edit"
                       title="编辑"
                       size="small"></el-button>
            <el-button type="danger"
                       icon="el-icon-delete"
                       title="删除"
                       size="small"></el-button>
            <el-button type="warning"
                       icon="el-icon-setting"
                       title="分配权限"
                       size="small"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[2,5, 10, 15, 20]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      userlist: [],
      total: 0
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    // 获取用户列表
    async getUserList () {
      const { data: res } = await this.$http.get('users', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.erroe('获取用户列表失败')
      }
      this.userlist = res.data.users
      this.total = res.data.total
      console.log(res)
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },
    async userStateChanged (userinfo) {
      const { data: res } = await this.$http.put(`users/${userinfo.id}/state/${userinfo.mg_state}`)
      if (res.meta.status !== 200) {
        userinfo.mg_state = !userinfo.mg_state
        return this.$message.error('更新用户状态失败')
      }
      this.$message.success('更新用户状态成功')
      console.log(res)
    }

  }
}
</script>
<style lang="less" scoped>
</style>
